package com.wsdc.encrption;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

/*
    对称加密  DES   AES 3DES
 */
public class MainActivity extends AppCompatActivity {
    private EditText content,pw1;

    private TextView text;
    private String mData;
    private String mPw;
    private String mData1;
    private String mPw2;
    private String mResult;
    private String mAnl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        //Log.d("flag", "onCreate: " +"你是怎么了");
    }

    private void initView() {
        content = (EditText) findViewById(R.id.main_content);
        pw1 = (EditText) findViewById(R.id.main_password);
        text = (TextView) findViewById(R.id.main_text);
    }

    //加密解密的点击事件
    public void encrypt(View view) {
        mResult = null;

        switch (view.getId()){
            case R.id.main_des://加密
                //加密的内容 加密的数据  密码

                mData = content.getText().toString().trim();

                mPw = pw1.getText().toString().trim();

                mResult = encrypt("DES", mData, mPw);

                text.setText(mResult);

                break;

            case R.id.main_dds://解密
                mData1 = text.getText().toString().trim();

                mPw2 = pw1.getText().toString().trim();

                mAnl = deEncrypt("DES", mData1, mPw2);

                text.setText(mAnl);


                break;

            case R.id.main_des3:
                mData = content.getText().toString().trim();

                mPw = pw1.getText().toString().trim();

                mResult = encrypt("DESede", mData, mPw);

                text.setText(mResult);

                break;


            case R.id.main_dds3:
                mData1 = text.getText().toString().trim();

                mPw2 = pw1.getText().toString().trim();

                mAnl = deEncrypt("DESede", mData1, mPw2);

                text.setText(mAnl);


                break;

            case R.id.main_aes:
                mData = content.getText().toString().trim();

                mPw = pw1.getText().toString().trim();

                mResult = encrypt("AES", mData, mPw);

                text.setText(mResult);
                break;

            case R.id.main_ads:
                mData1 = text.getText().toString().trim();

                mPw2 = pw1.getText().toString().trim();

                mAnl = deEncrypt("AES", mData1, mPw2);

                text.setText(mAnl);
                break;
        }
    }

    private String deEncrypt(String type, String data1, String pw2) {
        //加密有三种方式
        switch (type){
            case "DES":
                //加密对应的类
                try {
                    Cipher cipher = Cipher.getInstance(type);

                    if(pw2.getBytes().length != 8){
                        throw new Exception("the number of password not matcch");
                    }

                    Key key = new SecretKeySpec(pw2.getBytes(),type);

                    //初始化  指定密码     加密  密码
                    cipher.init(Cipher.DECRYPT_MODE,key);

                    //执行加密
                    if (!TextUtils.isEmpty(data1)) {
                        byte[] decode = Base64.decode(data1, Base64.DEFAULT);

                        byte[] bytes = cipher.doFinal(decode);


                        return new String(bytes);
                    }


                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }


                break;

            case "DESede":
                //加密对应的类
                try {
                    Cipher cipher = Cipher.getInstance(type);

                    if(pw2.getBytes().length != 24){
                        throw new Exception("the number of password not matcch");
                    }

                    Key key = new SecretKeySpec(pw2.getBytes(),type);

                    //初始化  指定密码     加密  密码
                    cipher.init(Cipher.DECRYPT_MODE,key);

                    //执行加密
                    if (!TextUtils.isEmpty(data1)) {
                        byte[] decode = Base64.decode(data1, Base64.DEFAULT);

                        byte[] bytes = cipher.doFinal(decode);


                        return new String(bytes);
                    }


                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;

            case "AES":
                //加密对应的类
                try {
                    Cipher cipher = Cipher.getInstance(type);

                    if(pw2.getBytes().length != 16){
                        throw new Exception("the number of password not matcch");
                    }

                    Key key = new SecretKeySpec(pw2.getBytes(),type);

                    //初始化  指定密码     加密  密码
                    cipher.init(Cipher.DECRYPT_MODE,key);

                    //执行加密
                    if (!TextUtils.isEmpty(data1)) {
                        byte[] decode = Base64.decode(data1, Base64.DEFAULT);

                        byte[] bytes = cipher.doFinal(decode);


                        return new String(bytes);
                    }


                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;

            default:
                try {
                    throw  new Exception("the encrypt type is not exist");
                } catch (Exception e) {
                    e.printStackTrace();
                }
        }
        return null;
    }

    private String encrypt(String type, String data, String pw) {
        //加密有三种方式
        switch (type){
            case "DES":
                //加密对应的类
                try {
                    Cipher cipher = Cipher.getInstance(type);

                    if(pw.getBytes().length != 8){
                        throw new Exception("the number of password not matcch");
                    }

                    Key key = new SecretKeySpec(pw.getBytes(),type);

                    //初始化  指定密码     加密  密码
                    cipher.init(Cipher.ENCRYPT_MODE,key);
                    Log.d("flag", "encrypt: " +"我在这里");

                    //执行加密
                    if (!TextUtils.isEmpty(data)) {
                        byte[] bytes = cipher.doFinal(data.getBytes());

                        byte[] encode = Base64.encode(bytes, Base64.DEFAULT);

                        return new String(encode);
                    }


                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }


                break;

            case "DESede":
                //加密对应的类
                try {
                    Cipher cipher = Cipher.getInstance(type);

                    if(pw.getBytes().length != 24){
                        throw new Exception("the number of password not matcch");
                    }

                    Key key = new SecretKeySpec(pw.getBytes(),type);

                    //初始化  指定密码     加密  密码
                    cipher.init(Cipher.ENCRYPT_MODE,key);
                    Log.d("flag", "encrypt: " +"我在这里");

                    //执行加密
                    if (!TextUtils.isEmpty(data)) {
                        byte[] bytes = cipher.doFinal(data.getBytes());

                        byte[] encode = Base64.encode(bytes, Base64.DEFAULT);

                        return new String(encode);
                    }


                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;

            case "AES":
                try {
                    Cipher cipher = Cipher.getInstance(type);

                    if(pw.getBytes().length != 16){
                        throw new Exception("the number of password not matcch");
                    }

                    Key key = new SecretKeySpec(pw.getBytes(),type);

                    //初始化  指定密码     加密  密码
                    cipher.init(Cipher.ENCRYPT_MODE,key);
                    Log.d("flag", "encrypt: " +"我在这里");

                    //执行加密
                    if (!TextUtils.isEmpty(data)) {
                        byte[] bytes = cipher.doFinal(data.getBytes());

                        byte[] encode = Base64.encode(bytes, Base64.DEFAULT);

                        return new String(encode);
                    }


                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;

            default:
                try {
                    throw  new Exception("the encrypt type is not exist");
                } catch (Exception e) {
                    e.printStackTrace();
                }
        }
        return null;
    }
}
